﻿using AssetManegement.Filter;
using Common;
using Microsoft.AspNetCore.Mvc;
using Model;
using ModelDto.Request;
using ModelDto.Response;
using ModelDto;
using Service.BaseService;
using Model.Sys;
using Service.SystemTableService;
using IService.SystemTableService;
using ModelDto.Request.SysTableDto;
using ModelDto.Response.SysTableDto;
using IService.BaseService;
using Newtonsoft.Json;

namespace AssetManegement.Controllers
{
    /// <summary>
    /// 打印模板hiprint版
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ServiceFilter(typeof(MyTokenFilterAttribute))]
    public class PrintTempForHiprintController : ControllerBase
    {
        private readonly IPrintTempForHiprintService printTempForHiprintService;
        public PrintTempForHiprintController(IPrintTempForHiprintService printTempForHiprintService)
        {
            this.printTempForHiprintService = printTempForHiprintService;
        }
        /// <summary>
        /// 根据ID获取对应打印模板
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResultL> GetPrintTempById(DeleteRequest request)
        {
            JsonResultL resultL = new JsonResultL();
            var data = await printTempForHiprintService.FindAsync(request.Id);
            if (data == null)
            {
                resultL.Fail("未找到打印模板,请联系管理员");
                return resultL;
            }

            return resultL.Sucess(EntityMapperHelper.Mapper<PrintTempForHiprintRequest, PrintTempForHiprint>(data));

        }
        /// <summary>
        /// 获取表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResultL> GetPrintTempList()
        {
            JsonResultL resultL = new JsonResultL();
            var data = await printTempForHiprintService.GetListCacheAsync();
            List<PrintTempForHiprintResponse> Response = new List<PrintTempForHiprintResponse>();
            if (data != null && data.Count > 0)
            {
                foreach (var item in data)
                {
                    Response.Add(EntityMapperHelper.Mapper<PrintTempForHiprintResponse, PrintTempForHiprint>(item));
                }
            }
            return resultL.Sucess(new { list = Response, total = Response.Count() });
        }
        /// <summary>
        /// 根据id删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> DeletePrintTemp(DeleteRequest request)
        {
            return await printTempForHiprintService.DeleteByWhereAsync(f => f.Id == request.Id);
        }
        /// <summary>
        /// 保存（新增或修改）
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResultL> SavePrintTemp(PrintTempForHiprintRequest request)
        {
            JsonResultL response = new JsonResultL();
            string tempUser = HttpContext.Request.Headers["LUserInfo"].ToString();
            AccountLoginInfo CurrentUser = JsonConvert.DeserializeObject<AccountLoginInfo>(tempUser);

            if (CurrentUser == null || CurrentUser.Account == null)
            {
                return response.TokenFail("用户异常");
            }
            if (string.IsNullOrEmpty(request.Id))
            {
                PrintTempForHiprint entity = new PrintTempForHiprint();
                entity.Type = request.Type;
                entity.Name = request.Name;

                entity.CreateUser = CurrentUser.Account.AccountNo;
                var flag = await printTempForHiprintService.AddAsync(entity);
                return flag ? response.Sucess("新增成功") : response.Fail("新增失败");
            }
            else
            {
                var entity = await printTempForHiprintService.FindAsync(request.Id);
                entity.Template = request.Template;
                entity.PaperWidth = request.PaperWidth;
                entity.PaperHeight = request.PaperHeight;
                entity.PaperType = request.PaperType;
                entity.UpdateTime = DateTime.Now;
                entity.UpdateUser = CurrentUser.Account.AccountNo;
                var flag = await printTempForHiprintService.UpdateAsync(entity);
                return flag ? response.Sucess("修改成功") : response.Fail("修改失败");
            }
        }
    }
}
